package helper.base.theListenerHelperTest

import helper.base.A2LHM
import org.junit.Test

internal class ListenerHelperTest1 {
    data class ZoneConstraint(
        val name: String,
        val constraint: String,
    )

    interface ZoneConstraintListener {
        fun onZoneConstraintAdded(
            zoneConstraint: ZoneConstraint
        )
    }

    class ZoneConstraintList : ArrayList<ZoneConstraint>(
    ) {
        private val listeners: ArrayList<ZoneConstraintListener> = ArrayList()

        fun addZoneConstraintListener(
            listener: ZoneConstraintListener
        ) {
            listeners.add(listener)
        }

        override fun add(
            element: ZoneConstraint
        ): Boolean {
            val added = super.add(element)
            if (added) {
                listeners.forEach {
                    it.onZoneConstraintAdded(element)
                }
            }
            return added
        }
    }

    @Test
    fun t1() {
        val zoneConstraints = ZoneConstraintList()
        val linkedHashMap = A2LHM<String, ZoneConstraint>()

        zoneConstraints.addZoneConstraintListener(
            object : ZoneConstraintListener {
                override fun onZoneConstraintAdded(
                    zoneConstraint: ZoneConstraint
                ) {
                    linkedHashMap[zoneConstraint.name] = zoneConstraint
                    // 执行其他操作
                }
            }
        )

        fun pln() {
            println("-----pln-----")
            println(zoneConstraints)
            println(linkedHashMap)
        }

        pln()
        zoneConstraints.add(
            ZoneConstraint(
                "n1",
                "c1"
            )
        )
        pln()
    }
}